home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / dkbtrace / pbmplus / source / pbm / macptopb.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-10  |  3.5 KB  |  139 lines

  1. /* macptopbm.c - read a MacPaint file and produce a portable bitmap
  2. **
  3. ** Copyright (C) 1988 by Jef Poskanzer.
  4. **
  5. ** Permission to use, copy, modify, and distribute this software and its
  6. ** documentation for any purpose and without fee is hereby granted, provided
  7. ** that the above copyright notice appear in all copies and that both that
  8. ** copyright notice and this permission notice appear in supporting
  9. ** documentation.  This software is provided "as is" without express or
  10. ** implied warranty.
  11. */
  12.  
  13. #include "pbm.h"
  14. #include "macp.h"
  15.  
  16. static void ReadMacPaintFile ARGS(( FILE* file, int extraskip, int* scanLineP, unsigned char Pic[MAX_LINES][BYTES_WIDE] ));
  17.  
  18. void
  19. main( argc, argv )
  20.     int argc;
  21.     char* argv[];
  22.     {
  23.     FILE* ifp;
  24.     unsigned char Pic[MAX_LINES][BYTES_WIDE];
  25.     bit* bitrow;
  26.     int argn, extraskip, scanLine, rows, cols, row, bcol, i;
  27.     char* usage = "[-extraskip N] [macpfile]";
  28.  
  29.     pbm_init( &argc, argv );
  30.  
  31.     argn = 1;
  32.     extraskip = 0;
  33.  
  34.     /* Check for flags. */
  35.     if ( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' )
  36.     {
  37.     if ( pm_keymatch( argv[argn], "-extraskip", 2 ) )
  38.         {
  39.         argn++;
  40.         if ( argn == argc || sscanf( argv[argn], "%d", &extraskip ) != 1 )
  41.         pm_usage( usage );
  42.         }
  43.     else
  44.         pm_usage( usage );
  45.     argn++;
  46.     }
  47.  
  48.     if ( argn < argc )
  49.     {
  50.     ifp = pm_openr( argv[argn] );
  51.     argn++;
  52.     }
  53.     else
  54.     ifp = stdin;
  55.  
  56.     if ( argn != argc )
  57.     pm_usage( usage );
  58.  
  59.     ReadMacPaintFile( ifp, extraskip, &scanLine, Pic );
  60.  
  61.     pm_close( ifp );
  62.  
  63.     cols = BYTES_WIDE * 8;
  64.     rows = scanLine;
  65.     pbm_writepbminit( stdout, cols, rows, 0 );
  66.     bitrow = pbm_allocrow( cols );
  67.  
  68.     for ( row = 0; row < rows; row++ )
  69.     {
  70.     for ( bcol = 0; bcol < BYTES_WIDE; bcol++ )
  71.         for ( i = 0; i < 8; i++ )
  72.         bitrow[bcol * 8 + i] =
  73.             ( (Pic[row][bcol] >> (7 - i)) & 1 ) ? PBM_BLACK : PBM_WHITE;
  74.     pbm_writepbmrow( stdout, bitrow, cols, 0 );
  75.     }
  76.  
  77.     pm_close( stdout );
  78.     exit( 0 );
  79.     }
  80.  
  81. /*
  82. ** Some of the following routine is:
  83. **
  84. **                Copyright 1987 by Patrick J. Naughton
  85. **                         All Rights Reserved
  86. ** Permission to use, copy, modify, and distribute this software and its
  87. ** documentation for any purpose and without fee is hereby granted,
  88. ** provided that the above copyright notice appear in all copies and that
  89. ** both that copyright notice and this permission notice appear in
  90. ** supporting documentation.
  91. */
  92.  
  93. static void
  94. ReadMacPaintFile( file, extraskip, scanLineP, Pic )
  95.     FILE* file;
  96.     int extraskip;
  97.     int* scanLineP;
  98.     unsigned char Pic[MAX_LINES][BYTES_WIDE];
  99.     {
  100.     unsigned int i, j, k;
  101.     unsigned char ch;
  102.  
  103.     /* Skip over the header. */
  104.     for ( i = 0; i < extraskip; i++ )
  105.     getc( file );
  106.     for ( i = 0; i < HEADER_LENGTH; i++ )
  107.     getc( file );
  108.  
  109.     *scanLineP = 0;
  110.     k = 0;
  111.  
  112.     while ( *scanLineP < MAX_LINES )
  113.     {
  114.     ch = (unsigned char) getc( file );    /* Count byte */
  115.     i = (unsigned int) ch;
  116.     if ( ch < 0x80 )
  117.         {    /* Unpack next (I+1) chars as is */
  118.         for ( j = 0; j <= i; j++ )
  119.         if ( *scanLineP < MAX_LINES )
  120.             {
  121.             Pic[*scanLineP][k++] = (unsigned char) getc( file );
  122.             if ( ! (k %= BYTES_WIDE) )
  123.             *scanLineP += 1;
  124.             }
  125.         }
  126.     else
  127.         {    /* Repeat next char (2's comp I) times */
  128.         ch = getc( file );
  129.         for ( j = 0; j <= 256 - i; j++ )
  130.         if ( *scanLineP < MAX_LINES )
  131.             {
  132.             Pic[*scanLineP][k++] = (unsigned char) ch;
  133.             if ( ! (k %= BYTES_WIDE) )
  134.             *scanLineP += 1;
  135.             }
  136.         }
  137.     }
  138.     }
  139.